// 引入Vue
import Vue from 'vue'
//引入VueRouter
import VueRouter from 'vue-router'

import routes from './routes'
//应用VueRouter
Vue.use(VueRouter)

//第一步：将VueRouter原型对象上的push缓存一份
const originPush = VueRouter.prototype.push
//第一步：将VueRouter原型对象上的replace缓存一份
const originReplace = VueRouter.prototype.replace

//第二步：增强push
VueRouter.prototype.push = function(location,okCallback,errCallback){
	//如果程序员调用this.$router.push的时候，只传了跳转的位置
	if(okCallback === undefined && errCallback === undefined){
		return originPush.call(this,location).catch(()=>{})
	}else{
		return originPush.call(this,location,okCallback,errCallback)
	}
}
//第二步：增强replace
VueRouter.prototype.replace = function(location,okCallback,errCallback){
	//如果程序员调用this.$router.push的时候，只传了跳转的位置
	if(okCallback === undefined && errCallback === undefined){
		return originReplace.call(this,location).catch(()=>{})
	}else{
		return originReplace.call(this,location,okCallback,errCallback)
	}
}

// 创建一个路由器
export default new VueRouter({
	mode:'history',
	routes,
	scrollBehavior (to, from, savedPosition) {
		if (savedPosition) {
		  return savedPosition
		} else {
		  return { x: 0, y: 100 }
		}
	  }
})


